{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1920db33",
   "metadata": {},
   "outputs": [],
   "source": [
    "options(repos = c(CRAN = \"https://mirrors.aliyun.com/CRAN/\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcc43865",
   "metadata": {},
   "source": [
    "### chapter4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "43ec1410",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ?data.frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c6711d43",
   "metadata": {},
   "outputs": [],
   "source": [
    "manager <- c(1, 2, 3, 4, 5)\n",
    "date <- c(\"10/24/08\", \"10/28/08\", \"10/1/08\", \"10/12/08\", \"5/1/09\")\n",
    "country <- c(\"US\", \"US\", \"UK\", \"UK\", \"UK\")\n",
    "gender <- c(\"M\", \"F\", \"F\", \"M\", \"F\")\n",
    "age <- c(32, 45, 25, 39, 99)\n",
    "q1 <- c(5, 3, 3, 3, 2)\n",
    "q2 <- c(4, 5, 5, 3, 2)\n",
    "q3 <- c(5, 2, 5, 4, 1)\n",
    "q4 <- c(5, 5, 5, NA, 2)\n",
    "q5 <- c(5, 5, 2, NA, 1)\n",
    "leadership <- data.frame(manager, date, country, gender, age,\n",
    "                          q1, q2, q3, q4, q5, stringsAsFactors=FALSE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0656b5d5",
   "metadata": {},
   "source": [
    "算术运算符\n",
    "\n",
    "<img src=\"img/021.png\" width=600 height=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0f5afe16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# a <- c(2,2,6,4)\n",
    "# b <- c(3,4,2,8)\n",
    "# mydata <- data.frame(a,b)\n",
    "# names(mydata) <- c(\"x1\",\"x2\")\n",
    "mydata <- data.frame(x1=c(3,4,2,8),x2=c(3,4,2,8))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4468e574",
   "metadata": {},
   "source": [
    "以下三种方式实现相同运算结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f0c46a7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  x1 x2 sumx meanx\n",
      "1  3  3    6     3\n",
      "2  4  4    8     4\n",
      "3  2  2    4     2\n",
      "4  8  8   16     8\n"
     ]
    }
   ],
   "source": [
    "mydata$sumx <- mydata$x1 + mydata$x2\n",
    "mydata$meanx <- mydata$sumx/2\n",
    "print(mydata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a78dbb02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  x1 x2 sumx meanx\n",
      "1  3  3    6     3\n",
      "2  4  4    8     4\n",
      "3  2  2    4     2\n",
      "4  8  8   16     8\n"
     ]
    }
   ],
   "source": [
    "attach(mydata)\n",
    "mydata$sumx <- x1+x2\n",
    "mydata$meanx <- (x1+x2)/2\n",
    "detach(mydata)\n",
    "print(mydata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e127f5d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  x1 x2 sumx meanx\n",
      "1  3  3    6     3\n",
      "2  4  4    8     4\n",
      "3  2  2    4     2\n",
      "4  8  8   16     8\n"
     ]
    }
   ],
   "source": [
    "mydata <- transform(mydata,sumx=x1+x2,meanx=(x1+x2)/2)\n",
    "print(mydata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2770a4f7",
   "metadata": {},
   "source": [
    "逻辑运算符\n",
    "\n",
    "<img src=\"img/028.png\" width=600 height=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5c27642b",
   "metadata": {},
   "outputs": [],
   "source": [
    "leadership$age[leadership$age==99] <- NA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e948080d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  manager     date country gender age q1 q2 q3 q4 q5 agecat\n",
      "1       1 10/24/08      US      M  32  5  4  5  5  5  Young\n",
      "2       2 10/28/08      US      F  45  3  5  2  5  5  Young\n",
      "3       3  10/1/08      UK      F  25  3  5  5  5  2  Young\n",
      "4       4 10/12/08      UK      M  39  3  3  4 NA NA  Young\n",
      "5       5   5/1/09      UK      F  NA  2  2  1  2  1   <NA>\n"
     ]
    }
   ],
   "source": [
    "leadership$agecat[leadership$age>=75] <- \"Elder\"\n",
    "leadership$agecat[leadership$age>=55 & leadership$age<75 ] <- \"Middle Aged\"\n",
    "leadership$agecat[leadership$age<55] <- \"Young\"\n",
    "print(leadership)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74e2cb7a",
   "metadata": {},
   "source": [
    "with压根没有对leadership进行操作，需要用within,会新返回一个dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "14bb5a08",
   "metadata": {},
   "outputs": [],
   "source": [
    "# leadership <- with(leadership,{\n",
    "#     agecat <- NA\n",
    "#     agecat[age>= 75] <-\"Elder\"\n",
    "#     agecat[age>= 55 & age<75] <-\"Middle Aged\"\n",
    "#     agecat[age<55] <-\"Young\"\n",
    "    \n",
    "# })\n",
    "# print(leadership)#很奇怪的一个返回\n",
    "# #[1] \"Young\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9a0d1928",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  manager     date country gender age q1 q2 q3 q4 q5 agecat\n",
      "1       1 10/24/08      US      M  32  5  4  5  5  5  Young\n",
      "2       2 10/28/08      US      F  45  3  5  2  5  5  Young\n",
      "3       3  10/1/08      UK      F  25  3  5  5  5  2  Young\n",
      "4       4 10/12/08      UK      M  39  3  3  4 NA NA  Young\n",
      "5       5   5/1/09      UK      F  NA  2  2  1  2  1   <NA>\n"
     ]
    }
   ],
   "source": [
    "leadership<-within(leadership,{\n",
    "    agecat <- NA\n",
    "    agecat[age>= 75] <-\"Elder\"\n",
    "    agecat[age>= 55 & age<75] <-\"Middle Aged\"\n",
    "    agecat[age<55] <-\"Young\"\n",
    "    \n",
    "})\n",
    "print(leadership)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d324f3e",
   "metadata": {},
   "source": [
    "可以通过fix修改dataframe的变量名或变量值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8ff2c716",
   "metadata": {},
   "outputs": [],
   "source": [
    "fix(leadership)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e1b7379",
   "metadata": {},
   "source": [
    "<img src=\"img/029.png\" width=300 height=200>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a61e121",
   "metadata": {},
   "source": [
    "单纯修改名称可以使用names函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "45451bda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  manager     date country gender age q1 q2 q3 q4 q5  ageC\n",
      "1       1 10/24/08      US      M  32  5  4  5  5  5 Young\n",
      "2       2 10/28/08      US      F  45  3  5  2  5  5 Young\n",
      "3       3  10/1/08      UK      F  25  3  5  5  5  2 Young\n",
      "4       4 10/12/08      UK      M  39  3  3  4 NA NA Young\n",
      "5       5   5/1/09      UK      F  NA  2  2  1  2  1  <NA>\n"
     ]
    }
   ],
   "source": [
    "names(leadership)[11] <- \"ageC\"\n",
    "print(leadership)\n",
    "#千万不能写成names(leadership)[-1]，否则意思是除了第一个其余都会被命名成ageC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "37dc2df6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  manager     date country gender age item1 item2 item3 item4 item5  ageC\n",
      "1       1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "2       2 10/28/08      US      F  45     3     5     2     5     5 Young\n",
      "3       3  10/1/08      UK      F  25     3     5     5     5     2 Young\n",
      "4       4 10/12/08      UK      M  39     3     3     4    NA    NA Young\n",
      "5       5   5/1/09      UK      F  NA     2     2     1     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "names(leadership)[6:10] <- c(\"item1\", \"item2\", \"item3\", \"item4\", \"item5\")\n",
    "print(leadership)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e75a4aa",
   "metadata": {},
   "source": [
    "plyr包中有一个rename()函数，可用于修改变量名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d71e390c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# install.packages(\"plyr\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "837834ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 5 × 11</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>managerID</th><th scope=col>testDate</th><th scope=col>country</th><th scope=col>gender</th><th scope=col>age</th><th scope=col>item1</th><th scope=col>item2</th><th scope=col>item3</th><th scope=col>item4</th><th scope=col>item5</th><th scope=col>ageC</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>1</td><td>10/24/08</td><td>US</td><td>M</td><td>32</td><td>5</td><td>4</td><td>5</td><td> 5</td><td> 5</td><td>Young</td></tr>\n",
       "\t<tr><th scope=row>2</th><td>2</td><td>10/28/08</td><td>US</td><td>F</td><td>45</td><td>3</td><td>5</td><td>2</td><td> 5</td><td> 5</td><td>Young</td></tr>\n",
       "\t<tr><th scope=row>3</th><td>3</td><td>10/1/08 </td><td>UK</td><td>F</td><td>25</td><td>3</td><td>5</td><td>5</td><td> 5</td><td> 2</td><td>Young</td></tr>\n",
       "\t<tr><th scope=row>4</th><td>4</td><td>10/12/08</td><td>UK</td><td>M</td><td>39</td><td>3</td><td>3</td><td>4</td><td>NA</td><td>NA</td><td>Young</td></tr>\n",
       "\t<tr><th scope=row>5</th><td>5</td><td>5/1/09  </td><td>UK</td><td>F</td><td>NA</td><td>2</td><td>2</td><td>1</td><td> 2</td><td> 1</td><td>NA   </td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 5 × 11\n",
       "\\begin{tabular}{r|lllllllllll}\n",
       "  & managerID & testDate & country & gender & age & item1 & item2 & item3 & item4 & item5 & ageC\\\\\n",
       "  & <dbl> & <chr> & <chr> & <chr> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <chr>\\\\\n",
       "\\hline\n",
       "\t1 & 1 & 10/24/08 & US & M & 32 & 5 & 4 & 5 &  5 &  5 & Young\\\\\n",
       "\t2 & 2 & 10/28/08 & US & F & 45 & 3 & 5 & 2 &  5 &  5 & Young\\\\\n",
       "\t3 & 3 & 10/1/08  & UK & F & 25 & 3 & 5 & 5 &  5 &  2 & Young\\\\\n",
       "\t4 & 4 & 10/12/08 & UK & M & 39 & 3 & 3 & 4 & NA & NA & Young\\\\\n",
       "\t5 & 5 & 5/1/09   & UK & F & NA & 2 & 2 & 1 &  2 &  1 & NA   \\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 5 × 11\n",
       "\n",
       "| <!--/--> | managerID &lt;dbl&gt; | testDate &lt;chr&gt; | country &lt;chr&gt; | gender &lt;chr&gt; | age &lt;dbl&gt; | item1 &lt;dbl&gt; | item2 &lt;dbl&gt; | item3 &lt;dbl&gt; | item4 &lt;dbl&gt; | item5 &lt;dbl&gt; | ageC &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|---|---|\n",
       "| 1 | 1 | 10/24/08 | US | M | 32 | 5 | 4 | 5 |  5 |  5 | Young |\n",
       "| 2 | 2 | 10/28/08 | US | F | 45 | 3 | 5 | 2 |  5 |  5 | Young |\n",
       "| 3 | 3 | 10/1/08  | UK | F | 25 | 3 | 5 | 5 |  5 |  2 | Young |\n",
       "| 4 | 4 | 10/12/08 | UK | M | 39 | 3 | 3 | 4 | NA | NA | Young |\n",
       "| 5 | 5 | 5/1/09   | UK | F | NA | 2 | 2 | 1 |  2 |  1 | NA    |\n",
       "\n"
      ],
      "text/plain": [
       "  managerID testDate country gender age item1 item2 item3 item4 item5 ageC \n",
       "1 1         10/24/08 US      M      32  5     4     5      5     5    Young\n",
       "2 2         10/28/08 US      F      45  3     5     2      5     5    Young\n",
       "3 3         10/1/08  UK      F      25  3     5     5      5     2    Young\n",
       "4 4         10/12/08 UK      M      39  3     3     4     NA    NA    Young\n",
       "5 5         5/1/09   UK      F      NA  2     2     1      2     1    NA   "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "library(plyr)\n",
    "leadership <- rename(leadership,c(manager=\"managerID\",date=\"testDate\"))\n",
    "leadership"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b928a8e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] FALSE FALSE FALSE  TRUE\n"
     ]
    }
   ],
   "source": [
    "y <- c(1, 2, 3, NA)\n",
    "print(is.na(y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5687741e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  item1 item2 item3 item4 item5\n",
      "1 FALSE FALSE FALSE FALSE FALSE\n",
      "2 FALSE FALSE FALSE FALSE FALSE\n",
      "3 FALSE FALSE FALSE FALSE FALSE\n",
      "4 FALSE FALSE FALSE  TRUE  TRUE\n",
      "5 FALSE FALSE FALSE FALSE FALSE\n"
     ]
    }
   ],
   "source": [
    "print(is.na(leadership[6:10]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "654f9163",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] NA\n",
      "[1] NA\n"
     ]
    }
   ],
   "source": [
    "x <- c(1, 2, NA, 3)\n",
    "y <- x[1]+x[2]+x[3]+x[4]\n",
    "z <- sum(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d8de62d",
   "metadata": {},
   "source": [
    "na.rm设为TRUE，就可以去除NA值后再进行计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "caa1eae2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "6"
      ],
      "text/latex": [
       "6"
      ],
      "text/markdown": [
       "6"
      ],
      "text/plain": [
       "[1] 6"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sum(x,na.rm=TRUE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "386f1227",
   "metadata": {},
   "source": [
    "na.omit()可以删除所有含有缺失数据的行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "45fc2950",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "2         2 10/28/08      US      F  45     3     5     2     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     5     5     2 Young\n"
     ]
    }
   ],
   "source": [
    "newdata <- na.omit(leadership)\n",
    "print(newdata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ed9edd",
   "metadata": {},
   "source": [
    "日期格式\n",
    "\n",
    "<img src=\"img/019.png\" width=600 height=400>\n",
    "\n",
    "日期值的默认输入格式为yyyy-mm-dd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cad66421",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"2007-06-22\" \"2004-02-13\"\n",
      "[1] \"Date\"\n"
     ]
    }
   ],
   "source": [
    "mydates <- as.Date(c(\"2007-06-22\", \"2004-02-13\"))\n",
    "print(mydates)\n",
    "print(class(mydates))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "98716d6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"1965-01-05\" \"1975-08-16\"\n"
     ]
    }
   ],
   "source": [
    "strDates <- c(\"01/05/1965\",\"08/16/1975\")\n",
    "dates <- as.Date(strDates,\"%m/%d/%Y\")\n",
    "print(dates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ac998d2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"2024-12-23\"\n"
     ]
    }
   ],
   "source": [
    "today <- Sys.Date()\n",
    "print(today)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "82ec33ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "'Mon Dec 23 17:02:05 2024'"
      ],
      "text/latex": [
       "'Mon Dec 23 17:02:05 2024'"
      ],
      "text/markdown": [
       "'Mon Dec 23 17:02:05 2024'"
      ],
      "text/plain": [
       "[1] \"Mon Dec 23 17:02:05 2024\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "date()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7072e72a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "'星期一'"
      ],
      "text/latex": [
       "'星期一'"
      ],
      "text/markdown": [
       "'星期一'"
      ],
      "text/plain": [
       "[1] \"星期一\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "format(today,format=\"%A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8cf46ea5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "'周一'"
      ],
      "text/latex": [
       "'周一'"
      ],
      "text/markdown": [
       "'周一'"
      ],
      "text/plain": [
       "[1] \"周一\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "format(today,format=\"%a\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87e6fff2",
   "metadata": {},
   "source": [
    "日期直接相减就是天数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "661446f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Time difference of 2535 days"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "startdate <- as.Date(\"2004-02-13\")\n",
    "enddate   <- as.Date(\"2011-01-22\")\n",
    "days      <- enddate - startdate\n",
    "days "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d1f3c80",
   "metadata": {},
   "source": [
    "difftime()来计算时间间隔，并以星期、天、时、分、秒来表示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "60d01050",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Time difference of 1530.429 weeks"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "today = Sys.Date()\n",
    "birday = as.Date(\"1995-08-25\")\n",
    "difftime(today,birday,unit=\"week\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "790ceb96",
   "metadata": {},
   "source": [
    "将日期转换成字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b54131a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>'1965-01-05'</li><li>'1975-08-16'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item '1965-01-05'\n",
       "\\item '1975-08-16'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. '1965-01-05'\n",
       "2. '1975-08-16'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"1965-01-05\" \"1975-08-16\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "strDates <- as.character(dates)\n",
    "strDates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaa2cd4c",
   "metadata": {},
   "source": [
    "类型转换\n",
    "\n",
    "<img src=\"img/020.png\" width=600 height=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6645c9fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] TRUE\n",
      "[1] TRUE\n",
      "[1] FALSE\n",
      "[1] TRUE\n",
      "[1] 1 2 3\n",
      "Levels: 1 2 3\n"
     ]
    }
   ],
   "source": [
    "a <- c(1,2,3)\n",
    "print(is.numeric(a))\n",
    "print(is.vector(a))\n",
    "print(is.factor(a))\n",
    "a <- as.factor(a)\n",
    "print(is.factor(a))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "17b2ba30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   managerID   testDate           country             gender         \n",
       " Min.   :1   Length:5           Length:5           Length:5          \n",
       " 1st Qu.:2   Class :character   Class :character   Class :character  \n",
       " Median :3   Mode  :character   Mode  :character   Mode  :character  \n",
       " Mean   :3                                                           \n",
       " 3rd Qu.:4                                                           \n",
       " Max.   :5                                                           "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "summary(leadership[1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9efcc896",
   "metadata": {},
   "source": [
    "因子的作用之一:相当于对类别型变量进行编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c6093d9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "leadership$gender = as.factor(leadership$gender)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9ffdcf9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   managerID   testDate           country          gender\n",
       " Min.   :1   Length:5           Length:5           F:3   \n",
       " 1st Qu.:2   Class :character   Class :character   M:2   \n",
       " Median :3   Mode  :character   Mode  :character         \n",
       " Mean   :3                                               \n",
       " 3rd Qu.:4                                               \n",
       " Max.   :5                                               "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "summary(leadership[1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f9ac4bc",
   "metadata": {},
   "source": [
    "order()函数进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "81fb5fe6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ?order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "23701671",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "2         2 10/28/08      US      F  45     3     5     2     5     5 Young\n",
      "4         4 10/12/08      UK      M  39     3     3     4    NA    NA Young\n",
      "1         1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     5     5     2 Young\n",
      "5         5   5/1/09      UK      F  NA     2     2     1     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "#按照年龄大小降序排序\n",
    "print(leadership[order(leadership$age,decreasing=TRUE),])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "8bd22334",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "3         3  10/1/08      UK      F  25     3     5     5     5     2 Young\n",
      "2         2 10/28/08      US      F  45     3     5     2     5     5 Young\n",
      "5         5   5/1/09      UK      F  NA     2     2     1     2     1  <NA>\n",
      "1         1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "4         4 10/12/08      UK      M  39     3     3     4    NA    NA Young\n"
     ]
    }
   ],
   "source": [
    "#按照性别，年龄排序\n",
    "print(leadership[order(leadership$gender,leadership$age),])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6711eae",
   "metadata": {},
   "source": [
    "数据合并\n",
    "\n",
    "- merge():按照某列横向合并\n",
    "\n",
    "- cbine():将相同行数的dataframe横向合并（不指定公共索引）\n",
    "\n",
    "- rbine():将有相同的变量的dataframe纵向合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2c8d71fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID country     date gender age q1 q2 q3 q4 q5\n",
      "1         1      US 10/24/08      M  32  5  4  5  5  5\n",
      "2         2      US 10/28/08      F  45  3  5  2  5  5\n",
      "3         3      UK  10/1/08      F  25  3  5  5  5  2\n",
      "4         4      UK 10/12/08      M  39  3  3  4 NA NA\n",
      "5         5      UK   5/1/09      F  99  2  2  1  2  1\n"
     ]
    }
   ],
   "source": [
    "managerID <- c(1, 2, 3, 4, 5)\n",
    "date <- c(\"10/24/08\", \"10/28/08\", \"10/1/08\", \"10/12/08\", \"5/1/09\")\n",
    "country <- c(\"US\", \"US\", \"UK\", \"UK\", \"UK\")\n",
    "gender <- c(\"M\", \"F\", \"F\", \"M\", \"F\")\n",
    "age <- c(32, 45, 25, 39, 99)\n",
    "q1 <- c(5, 3, 3, 3, 2)\n",
    "q2 <- c(4, 5, 5, 3, 2)\n",
    "q3 <- c(5, 2, 5, 4, 1)\n",
    "q4 <- c(5, 5, 5, NA, 2)\n",
    "q5 <- c(5, 5, 2, NA, 1)\n",
    "\n",
    "dfA = data.frame(managerID,date,country,gender,age)\n",
    "dfB = data.frame(managerID,country,q1,q2,q3,q4,q5)\n",
    "print(merge(dfA,dfB,by=c(\"managerID\",\"country\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b3227c08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID     date country gender age managerID country q1 q2 q3 q4 q5\n",
      "1         1 10/24/08      US      M  32         1      US  5  4  5  5  5\n",
      "2         2 10/28/08      US      F  45         2      US  3  5  2  5  5\n",
      "3         3  10/1/08      UK      F  25         3      UK  3  5  5  5  2\n",
      "4         4 10/12/08      UK      M  39         4      UK  3  3  4 NA NA\n",
      "5         5   5/1/09      UK      F  99         5      UK  2  2  1  2  1\n"
     ]
    }
   ],
   "source": [
    "print(cbind(dfA,dfB))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e5ea78e",
   "metadata": {},
   "source": [
    "数据集取子集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bac3cfce",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  item1 item2 item3 item4 item5\n",
      "1     5     4     5     5     5\n",
      "2     3     5     2     5     5\n",
      "3     3     5     5     5     2\n",
      "4     3     3     4    NA    NA\n",
      "5     2     2     1     2     1\n"
     ]
    }
   ],
   "source": [
    "print(leadership[,c(6:10)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "3bb64285",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  item1 item2 item3 item4 item5\n",
      "1     5     4     5     5     5\n",
      "2     3     5     2     5     5\n",
      "3     3     5     5     5     2\n",
      "4     3     3     4    NA    NA\n",
      "5     2     2     1     2     1\n"
     ]
    }
   ],
   "source": [
    "print(leadership[,paste(\"item\",1:5,sep=\"\")])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "13878130",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5 Young\n",
      "2         2 10/28/08      US      F  45     3     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     2 Young\n",
      "4         4 10/12/08      UK      M  39     3     3    NA Young\n",
      "5         5   5/1/09      UK      F  NA     2     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "myvars <- names(leadership) %in% c(\"item3\",\"item4\")\n",
    "print(leadership[!myvars])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "cd907c69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5 Young\n",
      "2         2 10/28/08      US      F  45     3     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     2 Young\n",
      "4         4 10/12/08      UK      M  39     3     3    NA Young\n",
      "5         5   5/1/09      UK      F  NA     2     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "#这个感觉很奇妙了，用减号加列名表示去除相应的列\n",
    "print(leadership[c(-8,-9)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8cb746b",
   "metadata": {},
   "source": [
    "上面删除某一列本质上还是选择列，通过赋值NULL，才是实际意义上删除某一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "6c99548f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_copy <- leadership"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5cf596b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "2         2 10/28/08      US      F  45     3     5     2     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     5     5     2 Young\n",
      "4         4 10/12/08      UK      M  39     3     3     4    NA    NA Young\n",
      "5         5   5/1/09      UK      F  NA     4     2     1     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "df_copy[5,\"item1\"] <- 4\n",
    "print(df_copy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a84f2a37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 1 2 3 4\n",
      "[1] 0 2 3 4\n"
     ]
    }
   ],
   "source": [
    "a <-c(1,2,3,4)\n",
    "b <- a\n",
    "b[1] <-0\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "244b71eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 1 2 3 4\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de3dae0c",
   "metadata": {},
   "source": [
    "原来4.1版本以后直接通过赋值就是深拷贝了，都不要用到copy或者clone方法（用了直接报错找不到这两种方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "adba840d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5 Young\n",
      "2         2 10/28/08      US      F  45     3     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     2 Young\n",
      "4         4 10/12/08      UK      M  39     3     3    NA Young\n",
      "5         5   5/1/09      UK      F  NA     4     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "df_copy$item3 <- df_copy$item4 <- NULL\n",
    "print(df_copy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "8dc2d229",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "2         2 10/28/08      US      F  45     3     5     2     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     5     5     2 Young\n",
      "4         4 10/12/08      UK      M  39     3     3     4    NA    NA Young\n",
      "5         5   5/1/09      UK      F  NA     2     2     1     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "print(leadership)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30a1c8ec",
   "metadata": {},
   "source": [
    "获取行的方式与获取列的方式类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "88bbd844",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "2         2 10/28/08      US      F  45     3     5     2     5     5 Young\n",
      "3         3  10/1/08      UK      F  25     3     5     5     5     2 Young\n"
     ]
    }
   ],
   "source": [
    "print(leadership[1:3,])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "04f0dce0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "1         1 10/24/08      US      M  32     5     4     5     5     5 Young\n",
      "4         4 10/12/08      UK      M  39     3     3     4    NA    NA Young\n"
     ]
    }
   ],
   "source": [
    "#其实就是获取满足条件的索引值，如果少了逗号，就会获取得到索引对应的列\n",
    "print(leadership[leadership$gender==\"M\" & leadership$age>30,])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "43858493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "'character'"
      ],
      "text/latex": [
       "'character'"
      ],
      "text/markdown": [
       "'character'"
      ],
      "text/plain": [
       "[1] \"character\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "class(leadership$testDate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "3c694913",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "'Date'"
      ],
      "text/latex": [
       "'Date'"
      ],
      "text/markdown": [
       "'Date'"
      ],
      "text/plain": [
       "[1] \"Date\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "leadership$testDate <- as.Date(leadership$testDate,\"%m/%d/%y\")\n",
    "class(leadership$testDate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "006e89e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID   testDate country gender age item1 item2 item3 item4 item5 ageC\n",
      "5         5 2009-05-01      UK      F  NA     2     2     1     2     1 <NA>\n"
     ]
    }
   ],
   "source": [
    "startdate <- as.Date(\"2009-01-01\")\n",
    "enddate <- as.Date(\"2009-10-31\")\n",
    "print(leadership[which(leadership$testDate>=startdate & leadership$testDate<=enddate),])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a45dde2d",
   "metadata": {},
   "source": [
    "subset()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "8db57082",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  item1 item2 item3\n",
      "1     5     4     5\n",
      "2     3     5     2\n",
      "4     3     3     4\n"
     ]
    }
   ],
   "source": [
    "print(subset(leadership,age>30|age<24,select=c(item1,item2,item3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "cf26423e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  gender age item1 item2 item3 item4\n",
      "1      M  32     5     4     5     5\n",
      "4      M  39     3     3     4    NA\n"
     ]
    }
   ],
   "source": [
    "print(subset(leadership,age>30&gender==\"M\",select=gender:item4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdc9e80e",
   "metadata": {},
   "source": [
    "随机抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "3fcc1d94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  managerID   testDate country gender age item1 item2 item3 item4 item5  ageC\n",
      "1         1 2008-10-24      US      M  32     5     4     5     5     5 Young\n",
      "5         5 2009-05-01      UK      F  NA     2     2     1     2     1  <NA>\n"
     ]
    }
   ],
   "source": [
    "print(leadership[sample(1:nrow(leadership),2,replace=FALSE),])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "645933d5",
   "metadata": {},
   "source": [
    "sample()函数中的第一个参数是一个由要从中抽样的元素组成的向量。在这里，这个向量是1到数据框中观测的数量，第二个参数是要抽取的元素数量，第三个参数表示无放回抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "8d3a524b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 1 3\n"
     ]
    }
   ],
   "source": [
    "print(sample(1:nrow(leadership),2,replace=FALSE))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5165a68a",
   "metadata": {},
   "source": [
    "使用SQL语句操作dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "72fa885d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# install.packages(\"sqldf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "4a8ac8d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading required package: gsubfn\n",
      "\n",
      "Loading required package: proto\n",
      "\n",
      "Loading required package: RSQLite\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  age gender item1 item2 item3\n",
      "1  32      M     5     4     5\n",
      "2  39      M     3     3     4\n"
     ]
    }
   ],
   "source": [
    "library(sqldf)\n",
    "print(sqldf(\"select age,gender,item1,item2,item3 from leadership where gender='M'\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52a081b6",
   "metadata": {},
   "source": [
    "### chapter5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b02055fd",
   "metadata": {},
   "source": [
    "数学函数\n",
    "\n",
    "<img src=\"img/022.png\" width=600 height=800>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faa91098",
   "metadata": {},
   "source": [
    "统计函数\n",
    "\n",
    "<img src=\"img/023.png\" width=600 height=800>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3408e912",
   "metadata": {},
   "source": [
    "概率函数\n",
    "\n",
    "<img src=\"img/024.png\" width=600 height=300>\n",
    "\n",
    "例如正态分布的有关函数，如果不指定一个均值和一个标准差，则函数将假定其为标准正态分布（均值为0，标准差为1）​。密度函数（dnorm）​、分布函数（pnorm）​、分位数函数（qnorm）和随机数生成函数（rnorm）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c0c7fb27",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA0gAAANICAMAAADKOT/pAAAAM1BMVEX9/v0AAABMTUxnaGd7\ne3uLjIuZmpmmpqaxsrG7vLvFxsXOz87X2Nff4N/n6Ofu7+79/v1tTElJAAAAEXRSTlP/////\n////////////////ACWtmWIAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAB84SURBVHic7d2JUhvL\nEkXR2xoQIDT8/9deJDEIjKQesqrynN4rXrzwtQFVZvdmxv7vCGCy/1ofAHBASEAAQgICEBIQ\ngJCAAIQEBCAkIAAhAQEICQhASEAAQgICEBIQgJCAAIQEBC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CEBIQABCAgIQEhCAkIAAhAQEICQgACEBAf4HioH790U0FQ0AAAAA\nSUVORK5CYII=",
      "text/plain": [
       "plot without title"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 420,
       "width": 420
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x <- pretty(c(-3,3),30)\n",
    "y <- dnorm(x)\n",
    "plot(x,y,type=\"l\",xlab=\"normal deviate\",ylab=\"density\",yaxs=\"i\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "fa36f69f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.97500210485178"
      ],
      "text/latex": [
       "0.97500210485178"
      ],
      "text/markdown": [
       "0.97500210485178"
      ],
      "text/plain": [
       "[1] 0.9750021"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#位于z=1.96左侧的标准正态分布的下方面积\n",
    "pnorm(1.96)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d1b71ac6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "628.15515655446"
      ],
      "text/latex": [
       "628.15515655446"
      ],
      "text/markdown": [
       "628.15515655446"
      ],
      "text/plain": [
       "[1] 628.1552"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#均值为500,标准差为100的正态分布的0.9分位点值是多少\n",
    "qnorm(0.9,mean=500,sd=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d3dfa63a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# #生成50个均值为50，标准差为10的正态随机数\n",
    "# rnorm(50,mean=50,sd=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "f7781519",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>0.0882054844405502</li><li>0.29782114806585</li><li>0.552359248744324</li><li>0.846109811216593</li><li>0.801747105782852</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 0.0882054844405502\n",
       "\\item 0.29782114806585\n",
       "\\item 0.552359248744324\n",
       "\\item 0.846109811216593\n",
       "\\item 0.801747105782852\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 0.0882054844405502\n",
       "2. 0.29782114806585\n",
       "3. 0.552359248744324\n",
       "4. 0.846109811216593\n",
       "5. 0.801747105782852\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] 0.08820548 0.29782115 0.55235925 0.84610981 0.80174711"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "runif(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e0b856aa-d73f-4e70-be1d-d84060bdf0da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 0.9753982 0.0772887 0.4178888 0.3927581 0.8983612\n",
      "[1] 0.2772913 0.4070026 0.7793815 0.8357256 0.2728074\n"
     ]
    }
   ],
   "source": [
    "print(runif(5))\n",
    "print(runif(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "d789d000-275e-4fb6-a007-3bae7ab7b129",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 0.71032245 0.24613730 0.38963444 0.09138367 0.96206454\n",
      "[1] 0.01093333 0.57429518 0.76439799 0.87338231 0.04106335\n"
     ]
    }
   ],
   "source": [
    "set.seed(13)\n",
    "print(runif(5))\n",
    "print(runif(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "79be053e-2436-4734-8562-fa6baa9eb312",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 0.71032245 0.24613730 0.38963444 0.09138367 0.96206454\n",
      "[1] 0.71032245 0.24613730 0.38963444 0.09138367 0.96206454\n"
     ]
    }
   ],
   "source": [
    "set.seed(13)\n",
    "print(runif(5))\n",
    "set.seed(13)#要再次设置相同的随机种子，才能确保生成的随机数相同\n",
    "print(runif(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3acf6aa2-e3e9-4e98-80af-1de33ec38a6a",
   "metadata": {},
   "source": [
    "生成多元正态数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "699aa106-a65b-44be-8519-2c3af0a70d74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] -2.293  0.721 -1.738  1.167  0.167\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>-2.29267612708539</li><li>0.720521422763161</li><li>-1.73847746945636</li><li>1.16688132383883</li><li>0.166912284061378</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item -2.29267612708539\n",
       "\\item 0.720521422763161\n",
       "\\item -1.73847746945636\n",
       "\\item 1.16688132383883\n",
       "\\item 0.166912284061378\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. -2.29267612708539\n",
       "2. 0.720521422763161\n",
       "3. -1.73847746945636\n",
       "4. 1.16688132383883\n",
       "5. 0.166912284061378\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] -2.293  0.721 -1.738  1.167  0.167"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "options(digits=3)\n",
    "a <- rnorm(5)\n",
    "print(a)#option设置保留小数位数，只有在print时候才能正常显示\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "17536298-a0a5-4841-bdb2-b5bb22c76e24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        [,1]   [,2]  [,3]\n",
      "[1,] 15360.8 6721.2 -47.1\n",
      "[2,]  6721.2 4700.9 -16.5\n",
      "[3,]   -47.1  -16.5   0.3\n"
     ]
    }
   ],
   "source": [
    "library(MASS)\n",
    "\n",
    "mean <- c(230.7,146.7,3.6)\n",
    "#sigma是一个正定矩阵，协方差的值是可以为负数的\n",
    "sigma <- matrix(c(15360.8,6721.2,-47.1,6721.2,4700.9,-16.5,-47.1,-16.5,0.3),nrow=3,ncol=3)\n",
    "print(sigma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "6d2bdc6c-d14b-4d63-8d9c-c9f9dbc867e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "mydata <- MASS::mvrnorm(500,mean,sigma)\n",
    "mydata <- as.data.frame(mydata)\n",
    "names(mydata) <- c(\"x\",\"y\",\"z\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "b4ad6c35-d7a2-487c-aa1a-d21beac97406",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 500   3\n",
      "    x     y    z\n",
      "1 206  89.9 3.76\n",
      "2 243 193.3 4.50\n",
      "3 308 133.9 2.87\n",
      "4 232 180.5 3.45\n",
      "5 227 175.6 3.75\n"
     ]
    }
   ],
   "source": [
    "print(dim(mydata))\n",
    "print(head(mydata,n=5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c718af5-aa4a-4d77-a832-3efb7c2b58f7",
   "metadata": {},
   "source": [
    "<img src=\"img/025.png\" width=600 height=300>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c41f84ca-22ba-47a8-80f6-9dec76c4d522",
   "metadata": {},
   "source": [
    "字符串处理\n",
    "\n",
    "<img src=\"img/027.png\" width=600 height=800>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "ba34dbec-2b97-4e42-9aa3-5b72b5326296",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 2 3 5\n"
     ]
    }
   ],
   "source": [
    "x <- c(\"ab\",\"cde\",\"fghab\")\n",
    "print(nchar(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "4e7f3041-46bb-4213-ad6b-8710846dfe7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"bcd\"\n"
     ]
    }
   ],
   "source": [
    "x <- \"abcdefg\"\n",
    "print(substr(x,2,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "fca019d3-fa79-441a-ba86-857709f5482d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"a222efg\"\n"
     ]
    }
   ],
   "source": [
    "x <- \"abcdefg\"\n",
    "substr(x,2,4) <- \"222\"\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32ce6855-5a8b-4a5e-8525-b76e0a680b6d",
   "metadata": {},
   "source": [
    "其他实用函数\n",
    "\n",
    "<img src=\"img/026.png\" width=600 height=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "1ae6121c-91ed-47d2-9ac6-950904964ef3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Bob .\n",
      "Isn't R great?"
     ]
    }
   ],
   "source": [
    "name <- \"Bob\"\n",
    "cat(\"Hello\",name,\"\\b.\\n\",\"\\bIsn\\'t R\",\"great?\")#为什么.前面的退格符号，貌似没有起到退格的作用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "23f55512-2808-492a-adc8-194bca086517",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Bob . \n",
      "Isn't R great?"
     ]
    }
   ],
   "source": [
    "cat(\"Hello\",name,\"\\b.\",\"\\n\",\"\\bIsn\\'t R\",\"great?\")#将转义字符分开写了以后，上述问题就不存在了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "28286dda-982f-4d54-8bde-57155c13d806",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 2.45\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "2.44948974278318"
      ],
      "text/latex": [
       "2.44948974278318"
      ],
      "text/markdown": [
       "2.44948974278318"
      ],
      "text/plain": [
       "[1] 2.45"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "options(digits=3)#保留3位有效数字？\n",
    "a <- 6\n",
    "print(sqrt(a))\n",
    "sqrt(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "4bd571cb-383e-4d20-844d-45c7476086f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 1 6 3\n"
     ]
    }
   ],
   "source": [
    "b <- c(1.243,5.654,2.99)\n",
    "print(round(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "32763952-1885-4d32-bc35-052b948b1221",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       [,1]   [,2]   [,3]   [,4]\n",
      "[1,] -0.984 -0.919 -0.597 -0.214\n",
      "[2,] -2.399 -1.250 -2.145 -1.097\n",
      "[3,] -0.460 -1.705 -0.148 -2.520\n"
     ]
    }
   ],
   "source": [
    "c <- matrix(runif(12),nrow=3)\n",
    "print(log(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "71bebccf-4927-4e4c-8a83-6c32c8481f49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 0.393\n"
     ]
    }
   ],
   "source": [
    "print(mean(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adda50c3-7250-4cbb-acbf-67ecf7039ea5",
   "metadata": {},
   "source": [
    "apply()函数，可将一个任意函数应用到矩阵、数组、DataFrame的任意维度上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "503043fc-8570-4365-893e-bb2749fe02ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        [,1]    [,2]   [,3]    [,4]    [,5]\n",
      "[1,] -0.5611  0.4255 -0.433 -1.7895 -0.7473\n",
      "[2,] -0.3994  0.5584  0.431  0.6318 -0.0709\n",
      "[3,]  0.8728 -1.1669  0.519  0.2656  0.7854\n",
      "[4,]  0.0944  1.0672  0.159 -0.7945 -3.6705\n",
      "[5,]  1.6359 -0.0986 -1.379  0.2821 -1.9145\n",
      "[6,]  0.7371  0.6315 -0.508 -0.0347 -1.6866\n"
     ]
    }
   ],
   "source": [
    "mydata <- matrix(rnorm(30),nrow=6)\n",
    "print(mydata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "dac67390-969d-4252-832b-ee9a4697861f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] -0.621  0.230  0.255 -0.629 -0.295 -0.172\n"
     ]
    }
   ],
   "source": [
    "print(apply(mydata,1,mean))#1就是按照行去求值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "6ade31bb-df82-4740-b37c-682bb0ed5e2d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] -0.621\n"
     ]
    }
   ],
   "source": [
    "print(mean(mydata[1,]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "6f232792-73f7-4100-8b3a-d90716a2bde4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]  0.397  0.236 -0.202 -0.240 -1.217\n"
     ]
    }
   ],
   "source": [
    "print(apply(mydata,2,mean))#按照列去求均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "362a7dee-6d8a-4d42-832a-71fffa49d299",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]  0.3262  0.3792 -0.0879 -0.0703 -1.1048\n"
     ]
    }
   ],
   "source": [
    "print(apply(mydata,2,mean,trim=0.2))#排序后用中间的60%的数字去求均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "0dec8f02-3c6c-4fe5-be78-4465571e975b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] -0.3994  0.0944  0.7371  0.8728\n"
     ]
    }
   ],
   "source": [
    "temp <- sort(mydata[,1])\n",
    "print(temp[2:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "5ef7a224-4087-4838-bf07-04c44ab9e2a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 1 2 4 6 3 5\n"
     ]
    }
   ],
   "source": [
    "print(order(mydata[,1]))#orde是对元素排序后返回排序后的index，sort才是对元素进行排序后返回元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "c49f0cbd-3ad4-416b-b6f0-8e0cadf2af8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 0.326\n"
     ]
    }
   ],
   "source": [
    "print(mean(temp[2:5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d21a7ba-4e74-4e77-837d-1aec5e69167d",
   "metadata": {},
   "source": [
    "将学生的各科成绩组合为单一的成绩衡量指标，基于相对名词给出从A到F的评分，根据学生的姓氏和名字的首字母对花名册进行排名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "d6ca307a-4a7b-43db-b13b-957be3ca02b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Student Math Science English\n",
      "1         John Davis  502      95      25\n",
      "2    Angela Williams  600      99      22\n",
      "3   Bullwinkle Moose  412      80      18\n",
      "4        David Jones  358      82      15\n",
      "5  Janice Markhammer  495      75      20\n",
      "6     Cheryl Cushing  512      85      28\n",
      "7     Reuven Ytzrhak  410      80      15\n",
      "8          Greg Knox  625      95      30\n",
      "9       Joel England  573      89      27\n",
      "10      Mary Rayburn  522      86      18\n"
     ]
    }
   ],
   "source": [
    "Student <- c(\"John Davis\",\"Angela Williams\",\"Bullwinkle Moose\",\"David Jones\",\"Janice Markhammer\",\"Cheryl Cushing\"\n",
    "            ,\"Reuven Ytzrhak\",\"Greg Knox\",\"Joel England\",\"Mary Rayburn\")\n",
    "Math <- c(502,600,412,358,495,512,410,625,573,522)\n",
    "Science <- c(95,99,80,82,75,85,80,95,89,86)\n",
    "English <- c(25,22,18,15,20,28,15,30,27,18)\n",
    "roster <- data.frame(Student, Math, Science, English)\n",
    "print(roster)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "17eed150-8f00-45a8-b545-8d5e7260b142",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores <- roster[,2:ncol(roster)]\n",
    "scores <- apply(scores,2,scale)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "fe45dbc9-7771-4fd3-a844-4317a04ac737",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Math Science English  zmean\n",
      "1   0.0127   1.078  0.5869  0.559\n",
      "2   1.1434   1.591  0.0367  0.924\n",
      "3  -1.0257  -0.847 -0.6969 -0.857\n",
      "4  -1.6487  -0.590 -1.2471 -1.162\n",
      "5  -0.0681  -1.489 -0.3301 -0.629\n",
      "6   0.1281  -0.205  1.1370  0.353\n",
      "7  -1.0488  -0.847 -1.2471 -1.048\n",
      "8   1.4318   1.078  1.5038  1.338\n",
      "9   0.8319   0.308  0.9536  0.698\n",
      "10  0.2434  -0.077 -0.6969 -0.177\n"
     ]
    }
   ],
   "source": [
    "scores = transform(scores,zmean = apply(scores,1,mean))\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "5572398b-6116-4e3e-a912-1606ac2d531f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Student Math Science English   MathZ ScienceZ EnglishZ  Zmean\n",
      "1         John Davis  502      95      25  0.0127    1.078   0.5869  0.559\n",
      "2    Angela Williams  600      99      22  1.1434    1.591   0.0367  0.924\n",
      "3   Bullwinkle Moose  412      80      18 -1.0257   -0.847  -0.6969 -0.857\n",
      "4        David Jones  358      82      15 -1.6487   -0.590  -1.2471 -1.162\n",
      "5  Janice Markhammer  495      75      20 -0.0681   -1.489  -0.3301 -0.629\n",
      "6     Cheryl Cushing  512      85      28  0.1281   -0.205   1.1370  0.353\n",
      "7     Reuven Ytzrhak  410      80      15 -1.0488   -0.847  -1.2471 -1.048\n",
      "8          Greg Knox  625      95      30  1.4318    1.078   1.5038  1.338\n",
      "9       Joel England  573      89      27  0.8319    0.308   0.9536  0.698\n",
      "10      Mary Rayburn  522      86      18  0.2434   -0.077  -0.6969 -0.177\n"
     ]
    }
   ],
   "source": [
    "names(scores) <- c(\"MathZ\",\"ScienceZ\",\"EnglishZ\",\"Zmean\")\n",
    "student_scores <- cbind(roster,scores)\n",
    "print(student_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "171554b5-65be-4088-91da-7592630fed1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0%    20%    40%    60%    80%   100% \n",
      "-1.162 -0.895 -0.358  0.436  0.743  1.338 \n"
     ]
    }
   ],
   "source": [
    "print(quantile(student_scores$Zmean,probs=seq(0,1,0.2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "7eee0edd-8ea3-473d-a1f3-632d9436e1b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# change_grade <- function(x,levels=quantile(student_scores$Zmean,probs=seq(0,1,0.2))){\n",
    "#     if (x<=levels[2])\n",
    "#         res <- \"F\"\n",
    "#     else if (x<=levels[3])\n",
    "#         res <- \"D\"\n",
    "#     else if (x<= levels[4])\n",
    "#         res <- \"C\"\n",
    "#     else if (x<= levels[5])\n",
    "#         res <- \"B\"\n",
    "#     else\n",
    "#         res <- \"A\"\n",
    "#     return (res)\n",
    "# }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "4fa2df76-e2b3-4fe1-bb9b-2c877a9694d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "levels <- quantile(student_scores$Zmean,probs=seq(0,1,0.2))\n",
    "student_scores <-within(student_scores,{\n",
    "    grade<-NA\n",
    "    grade[Zmean<=levels[2]] <- 'F'\n",
    "    grade[Zmean<=levels[3] & Zmean>levels[2]] <- 'D'\n",
    "    grade[Zmean<=levels[4] & Zmean>levels[3]] <- 'C'\n",
    "    grade[Zmean<=levels[5] & Zmean>levels[4]] <- 'B'\n",
    "    grade[Zmean>levels[5]] <- 'A'\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "b114afc0-2b54-4fdb-a3d9-a921b0d5f0f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 10 × 9</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>Student</th><th scope=col>Math</th><th scope=col>Science</th><th scope=col>English</th><th scope=col>MathZ</th><th scope=col>ScienceZ</th><th scope=col>EnglishZ</th><th scope=col>Zmean</th><th scope=col>grade</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>John Davis       </td><td>502</td><td>95</td><td>25</td><td> 0.0127</td><td> 1.078</td><td> 0.5869</td><td> 0.559</td><td>B</td></tr>\n",
       "\t<tr><td>Angela Williams  </td><td>600</td><td>99</td><td>22</td><td> 1.1434</td><td> 1.591</td><td> 0.0367</td><td> 0.924</td><td>A</td></tr>\n",
       "\t<tr><td>Bullwinkle Moose </td><td>412</td><td>80</td><td>18</td><td>-1.0257</td><td>-0.847</td><td>-0.6969</td><td>-0.857</td><td>D</td></tr>\n",
       "\t<tr><td>David Jones      </td><td>358</td><td>82</td><td>15</td><td>-1.6487</td><td>-0.590</td><td>-1.2471</td><td>-1.162</td><td>F</td></tr>\n",
       "\t<tr><td>Janice Markhammer</td><td>495</td><td>75</td><td>20</td><td>-0.0681</td><td>-1.489</td><td>-0.3301</td><td>-0.629</td><td>D</td></tr>\n",
       "\t<tr><td>Cheryl Cushing   </td><td>512</td><td>85</td><td>28</td><td> 0.1281</td><td>-0.205</td><td> 1.1370</td><td> 0.353</td><td>C</td></tr>\n",
       "\t<tr><td>Reuven Ytzrhak   </td><td>410</td><td>80</td><td>15</td><td>-1.0488</td><td>-0.847</td><td>-1.2471</td><td>-1.048</td><td>F</td></tr>\n",
       "\t<tr><td>Greg Knox        </td><td>625</td><td>95</td><td>30</td><td> 1.4318</td><td> 1.078</td><td> 1.5038</td><td> 1.338</td><td>A</td></tr>\n",
       "\t<tr><td>Joel England     </td><td>573</td><td>89</td><td>27</td><td> 0.8319</td><td> 0.308</td><td> 0.9536</td><td> 0.698</td><td>B</td></tr>\n",
       "\t<tr><td>Mary Rayburn     </td><td>522</td><td>86</td><td>18</td><td> 0.2434</td><td>-0.077</td><td>-0.6969</td><td>-0.177</td><td>C</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 10 × 9\n",
       "\\begin{tabular}{lllllllll}\n",
       " Student & Math & Science & English & MathZ & ScienceZ & EnglishZ & Zmean & grade\\\\\n",
       " <chr> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <chr>\\\\\n",
       "\\hline\n",
       "\t John Davis        & 502 & 95 & 25 &  0.0127 &  1.078 &  0.5869 &  0.559 & B\\\\\n",
       "\t Angela Williams   & 600 & 99 & 22 &  1.1434 &  1.591 &  0.0367 &  0.924 & A\\\\\n",
       "\t Bullwinkle Moose  & 412 & 80 & 18 & -1.0257 & -0.847 & -0.6969 & -0.857 & D\\\\\n",
       "\t David Jones       & 358 & 82 & 15 & -1.6487 & -0.590 & -1.2471 & -1.162 & F\\\\\n",
       "\t Janice Markhammer & 495 & 75 & 20 & -0.0681 & -1.489 & -0.3301 & -0.629 & D\\\\\n",
       "\t Cheryl Cushing    & 512 & 85 & 28 &  0.1281 & -0.205 &  1.1370 &  0.353 & C\\\\\n",
       "\t Reuven Ytzrhak    & 410 & 80 & 15 & -1.0488 & -0.847 & -1.2471 & -1.048 & F\\\\\n",
       "\t Greg Knox         & 625 & 95 & 30 &  1.4318 &  1.078 &  1.5038 &  1.338 & A\\\\\n",
       "\t Joel England      & 573 & 89 & 27 &  0.8319 &  0.308 &  0.9536 &  0.698 & B\\\\\n",
       "\t Mary Rayburn      & 522 & 86 & 18 &  0.2434 & -0.077 & -0.6969 & -0.177 & C\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 10 × 9\n",
       "\n",
       "| Student &lt;chr&gt; | Math &lt;dbl&gt; | Science &lt;dbl&gt; | English &lt;dbl&gt; | MathZ &lt;dbl&gt; | ScienceZ &lt;dbl&gt; | EnglishZ &lt;dbl&gt; | Zmean &lt;dbl&gt; | grade &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|\n",
       "| John Davis        | 502 | 95 | 25 |  0.0127 |  1.078 |  0.5869 |  0.559 | B |\n",
       "| Angela Williams   | 600 | 99 | 22 |  1.1434 |  1.591 |  0.0367 |  0.924 | A |\n",
       "| Bullwinkle Moose  | 412 | 80 | 18 | -1.0257 | -0.847 | -0.6969 | -0.857 | D |\n",
       "| David Jones       | 358 | 82 | 15 | -1.6487 | -0.590 | -1.2471 | -1.162 | F |\n",
       "| Janice Markhammer | 495 | 75 | 20 | -0.0681 | -1.489 | -0.3301 | -0.629 | D |\n",
       "| Cheryl Cushing    | 512 | 85 | 28 |  0.1281 | -0.205 |  1.1370 |  0.353 | C |\n",
       "| Reuven Ytzrhak    | 410 | 80 | 15 | -1.0488 | -0.847 | -1.2471 | -1.048 | F |\n",
       "| Greg Knox         | 625 | 95 | 30 |  1.4318 |  1.078 |  1.5038 |  1.338 | A |\n",
       "| Joel England      | 573 | 89 | 27 |  0.8319 |  0.308 |  0.9536 |  0.698 | B |\n",
       "| Mary Rayburn      | 522 | 86 | 18 |  0.2434 | -0.077 | -0.6969 | -0.177 | C |\n",
       "\n"
      ],
      "text/plain": [
       "   Student           Math Science English MathZ   ScienceZ EnglishZ Zmean \n",
       "1  John Davis        502  95      25       0.0127  1.078    0.5869   0.559\n",
       "2  Angela Williams   600  99      22       1.1434  1.591    0.0367   0.924\n",
       "3  Bullwinkle Moose  412  80      18      -1.0257 -0.847   -0.6969  -0.857\n",
       "4  David Jones       358  82      15      -1.6487 -0.590   -1.2471  -1.162\n",
       "5  Janice Markhammer 495  75      20      -0.0681 -1.489   -0.3301  -0.629\n",
       "6  Cheryl Cushing    512  85      28       0.1281 -0.205    1.1370   0.353\n",
       "7  Reuven Ytzrhak    410  80      15      -1.0488 -0.847   -1.2471  -1.048\n",
       "8  Greg Knox         625  95      30       1.4318  1.078    1.5038   1.338\n",
       "9  Joel England      573  89      27       0.8319  0.308    0.9536   0.698\n",
       "10 Mary Rayburn      522  86      18       0.2434 -0.077   -0.6969  -0.177\n",
       "   grade\n",
       "1  B    \n",
       "2  A    \n",
       "3  D    \n",
       "4  F    \n",
       "5  D    \n",
       "6  C    \n",
       "7  F    \n",
       "8  A    \n",
       "9  B    \n",
       "10 C    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "student_scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "cc915734-4902-4484-bd86-e66978c6c704",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]]\n",
      "[1] \"a\" \"b\" \"c\"\n",
      "\n",
      "[[2]]\n",
      "[1] \"d\" \"e\" \"f\" \"g\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "x <- c(\"abc\",\"defg\")\n",
    "y <-strsplit(x,split=\"\")\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "f43d02d3-f636-4491-9a66-c5069909f423",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"b\" \"e\"\n"
     ]
    }
   ],
   "source": [
    "print(sapply(y,\"[\",2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "78b29022-fb17-47ef-855d-4c078b41146f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    FirstName   LastName           Student Math Science English   MathZ\n",
      "1        John      Davis        John Davis  502      95      25  0.0127\n",
      "2      Angela   Williams   Angela Williams  600      99      22  1.1434\n",
      "3  Bullwinkle      Moose  Bullwinkle Moose  412      80      18 -1.0257\n",
      "4       David      Jones       David Jones  358      82      15 -1.6487\n",
      "5      Janice Markhammer Janice Markhammer  495      75      20 -0.0681\n",
      "6      Cheryl    Cushing    Cheryl Cushing  512      85      28  0.1281\n",
      "7      Reuven    Ytzrhak    Reuven Ytzrhak  410      80      15 -1.0488\n",
      "8        Greg       Knox         Greg Knox  625      95      30  1.4318\n",
      "9        Joel    England      Joel England  573      89      27  0.8319\n",
      "10       Mary    Rayburn      Mary Rayburn  522      86      18  0.2434\n",
      "   ScienceZ EnglishZ  Zmean grade\n",
      "1     1.078   0.5869  0.559     B\n",
      "2     1.591   0.0367  0.924     A\n",
      "3    -0.847  -0.6969 -0.857     D\n",
      "4    -0.590  -1.2471 -1.162     F\n",
      "5    -1.489  -0.3301 -0.629     D\n",
      "6    -0.205   1.1370  0.353     C\n",
      "7    -0.847  -1.2471 -1.048     F\n",
      "8     1.078   1.5038  1.338     A\n",
      "9     0.308   0.9536  0.698     B\n",
      "10   -0.077  -0.6969 -0.177     C\n"
     ]
    }
   ],
   "source": [
    "Names <- strsplit(student_scores$Student,split=\" \")\n",
    "FirstName <- sapply(Names,\"[\",1)\n",
    "LastName <- sapply(Names,\"[\",2)\n",
    "student_names = data.frame(FirstName,LastName)\n",
    "students <- cbind(student_names,student_scores)\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "8f55dd35-e02a-4c34-b6c2-d64c225d9ef6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 10 × 11</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>FirstName</th><th scope=col>LastName</th><th scope=col>Student</th><th scope=col>Math</th><th scope=col>Science</th><th scope=col>English</th><th scope=col>MathZ</th><th scope=col>ScienceZ</th><th scope=col>EnglishZ</th><th scope=col>Zmean</th><th scope=col>grade</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>6</th><td>Cheryl    </td><td>Cushing   </td><td>Cheryl Cushing   </td><td>512</td><td>85</td><td>28</td><td> 0.1281</td><td>-0.205</td><td> 1.1370</td><td> 0.353</td><td>C</td></tr>\n",
       "\t<tr><th scope=row>1</th><td>John      </td><td>Davis     </td><td>John Davis       </td><td>502</td><td>95</td><td>25</td><td> 0.0127</td><td> 1.078</td><td> 0.5869</td><td> 0.559</td><td>B</td></tr>\n",
       "\t<tr><th scope=row>9</th><td>Joel      </td><td>England   </td><td>Joel England     </td><td>573</td><td>89</td><td>27</td><td> 0.8319</td><td> 0.308</td><td> 0.9536</td><td> 0.698</td><td>B</td></tr>\n",
       "\t<tr><th scope=row>4</th><td>David     </td><td>Jones     </td><td>David Jones      </td><td>358</td><td>82</td><td>15</td><td>-1.6487</td><td>-0.590</td><td>-1.2471</td><td>-1.162</td><td>F</td></tr>\n",
       "\t<tr><th scope=row>8</th><td>Greg      </td><td>Knox      </td><td>Greg Knox        </td><td>625</td><td>95</td><td>30</td><td> 1.4318</td><td> 1.078</td><td> 1.5038</td><td> 1.338</td><td>A</td></tr>\n",
       "\t<tr><th scope=row>5</th><td>Janice    </td><td>Markhammer</td><td>Janice Markhammer</td><td>495</td><td>75</td><td>20</td><td>-0.0681</td><td>-1.489</td><td>-0.3301</td><td>-0.629</td><td>D</td></tr>\n",
       "\t<tr><th scope=row>3</th><td>Bullwinkle</td><td>Moose     </td><td>Bullwinkle Moose </td><td>412</td><td>80</td><td>18</td><td>-1.0257</td><td>-0.847</td><td>-0.6969</td><td>-0.857</td><td>D</td></tr>\n",
       "\t<tr><th scope=row>10</th><td>Mary      </td><td>Rayburn   </td><td>Mary Rayburn     </td><td>522</td><td>86</td><td>18</td><td> 0.2434</td><td>-0.077</td><td>-0.6969</td><td>-0.177</td><td>C</td></tr>\n",
       "\t<tr><th scope=row>2</th><td>Angela    </td><td>Williams  </td><td>Angela Williams  </td><td>600</td><td>99</td><td>22</td><td> 1.1434</td><td> 1.591</td><td> 0.0367</td><td> 0.924</td><td>A</td></tr>\n",
       "\t<tr><th scope=row>7</th><td>Reuven    </td><td>Ytzrhak   </td><td>Reuven Ytzrhak   </td><td>410</td><td>80</td><td>15</td><td>-1.0488</td><td>-0.847</td><td>-1.2471</td><td>-1.048</td><td>F</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 10 × 11\n",
       "\\begin{tabular}{r|lllllllllll}\n",
       "  & FirstName & LastName & Student & Math & Science & English & MathZ & ScienceZ & EnglishZ & Zmean & grade\\\\\n",
       "  & <chr> & <chr> & <chr> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <chr>\\\\\n",
       "\\hline\n",
       "\t6 & Cheryl     & Cushing    & Cheryl Cushing    & 512 & 85 & 28 &  0.1281 & -0.205 &  1.1370 &  0.353 & C\\\\\n",
       "\t1 & John       & Davis      & John Davis        & 502 & 95 & 25 &  0.0127 &  1.078 &  0.5869 &  0.559 & B\\\\\n",
       "\t9 & Joel       & England    & Joel England      & 573 & 89 & 27 &  0.8319 &  0.308 &  0.9536 &  0.698 & B\\\\\n",
       "\t4 & David      & Jones      & David Jones       & 358 & 82 & 15 & -1.6487 & -0.590 & -1.2471 & -1.162 & F\\\\\n",
       "\t8 & Greg       & Knox       & Greg Knox         & 625 & 95 & 30 &  1.4318 &  1.078 &  1.5038 &  1.338 & A\\\\\n",
       "\t5 & Janice     & Markhammer & Janice Markhammer & 495 & 75 & 20 & -0.0681 & -1.489 & -0.3301 & -0.629 & D\\\\\n",
       "\t3 & Bullwinkle & Moose      & Bullwinkle Moose  & 412 & 80 & 18 & -1.0257 & -0.847 & -0.6969 & -0.857 & D\\\\\n",
       "\t10 & Mary       & Rayburn    & Mary Rayburn      & 522 & 86 & 18 &  0.2434 & -0.077 & -0.6969 & -0.177 & C\\\\\n",
       "\t2 & Angela     & Williams   & Angela Williams   & 600 & 99 & 22 &  1.1434 &  1.591 &  0.0367 &  0.924 & A\\\\\n",
       "\t7 & Reuven     & Ytzrhak    & Reuven Ytzrhak    & 410 & 80 & 15 & -1.0488 & -0.847 & -1.2471 & -1.048 & F\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 10 × 11\n",
       "\n",
       "| <!--/--> | FirstName &lt;chr&gt; | LastName &lt;chr&gt; | Student &lt;chr&gt; | Math &lt;dbl&gt; | Science &lt;dbl&gt; | English &lt;dbl&gt; | MathZ &lt;dbl&gt; | ScienceZ &lt;dbl&gt; | EnglishZ &lt;dbl&gt; | Zmean &lt;dbl&gt; | grade &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|---|---|\n",
       "| 6 | Cheryl     | Cushing    | Cheryl Cushing    | 512 | 85 | 28 |  0.1281 | -0.205 |  1.1370 |  0.353 | C |\n",
       "| 1 | John       | Davis      | John Davis        | 502 | 95 | 25 |  0.0127 |  1.078 |  0.5869 |  0.559 | B |\n",
       "| 9 | Joel       | England    | Joel England      | 573 | 89 | 27 |  0.8319 |  0.308 |  0.9536 |  0.698 | B |\n",
       "| 4 | David      | Jones      | David Jones       | 358 | 82 | 15 | -1.6487 | -0.590 | -1.2471 | -1.162 | F |\n",
       "| 8 | Greg       | Knox       | Greg Knox         | 625 | 95 | 30 |  1.4318 |  1.078 |  1.5038 |  1.338 | A |\n",
       "| 5 | Janice     | Markhammer | Janice Markhammer | 495 | 75 | 20 | -0.0681 | -1.489 | -0.3301 | -0.629 | D |\n",
       "| 3 | Bullwinkle | Moose      | Bullwinkle Moose  | 412 | 80 | 18 | -1.0257 | -0.847 | -0.6969 | -0.857 | D |\n",
       "| 10 | Mary       | Rayburn    | Mary Rayburn      | 522 | 86 | 18 |  0.2434 | -0.077 | -0.6969 | -0.177 | C |\n",
       "| 2 | Angela     | Williams   | Angela Williams   | 600 | 99 | 22 |  1.1434 |  1.591 |  0.0367 |  0.924 | A |\n",
       "| 7 | Reuven     | Ytzrhak    | Reuven Ytzrhak    | 410 | 80 | 15 | -1.0488 | -0.847 | -1.2471 | -1.048 | F |\n",
       "\n"
      ],
      "text/plain": [
       "   FirstName  LastName   Student           Math Science English MathZ  \n",
       "6  Cheryl     Cushing    Cheryl Cushing    512  85      28       0.1281\n",
       "1  John       Davis      John Davis        502  95      25       0.0127\n",
       "9  Joel       England    Joel England      573  89      27       0.8319\n",
       "4  David      Jones      David Jones       358  82      15      -1.6487\n",
       "8  Greg       Knox       Greg Knox         625  95      30       1.4318\n",
       "5  Janice     Markhammer Janice Markhammer 495  75      20      -0.0681\n",
       "3  Bullwinkle Moose      Bullwinkle Moose  412  80      18      -1.0257\n",
       "10 Mary       Rayburn    Mary Rayburn      522  86      18       0.2434\n",
       "2  Angela     Williams   Angela Williams   600  99      22       1.1434\n",
       "7  Reuven     Ytzrhak    Reuven Ytzrhak    410  80      15      -1.0488\n",
       "   ScienceZ EnglishZ Zmean  grade\n",
       "6  -0.205    1.1370   0.353 C    \n",
       "1   1.078    0.5869   0.559 B    \n",
       "9   0.308    0.9536   0.698 B    \n",
       "4  -0.590   -1.2471  -1.162 F    \n",
       "8   1.078    1.5038   1.338 A    \n",
       "5  -1.489   -0.3301  -0.629 D    \n",
       "3  -0.847   -0.6969  -0.857 D    \n",
       "10 -0.077   -0.6969  -0.177 C    \n",
       "2   1.591    0.0367   0.924 A    \n",
       "7  -0.847   -1.2471  -1.048 F    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "students <- students[order(students$LastName,students$LastName),]\n",
    "students"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c638e912-ae87-44fa-85d6-82c5545e1c59",
   "metadata": {},
   "source": [
    "控制流"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "73d7303d-4d50-4b4c-a3b5-29d68007a439",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"hello\"\n",
      "[1] \"R\"\n"
     ]
    }
   ],
   "source": [
    "for (i in c(\"hello\",\"R\"))\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "366fcef2-e419-4209-8b19-9c52c505bc99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 1\n",
      "[1] 2\n",
      "[1] 3\n"
     ]
    }
   ],
   "source": [
    "for (i in 1:3)\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "6935ce12-3b1f-456e-b47e-0fe38816125c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello 3 \n",
      "hello 2 \n",
      "hello 1 \n"
     ]
    }
   ],
   "source": [
    "i <- 3\n",
    "while (i>0){\n",
    "    cat(\"hello\",i,\"\\n\")\n",
    "    i <- i-1\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "c061a67a-7aa2-4d85-aa98-b340ee6a7fc3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"A\"\n"
     ]
    }
   ],
   "source": [
    "score <- 99\n",
    "if (score>90){\n",
    "    print(\"A\")\n",
    "}else if (score>80){\n",
    "    print(\"B\")\n",
    "}else if (score>70){\n",
    "    print(\"C\")\n",
    "}else if (score>60){\n",
    "    print(\"D\")\n",
    "}else\n",
    "    print(\"F\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4816cd0d-29de-4de3-96de-3457a402aa81",
   "metadata": {},
   "source": [
    "很奇怪，如果换行一定要上面这么写，如果没有加上花括号直接报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "04fe833b-b773-4477-8fc0-97a7e584e33f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"A\"\n"
     ]
    }
   ],
   "source": [
    "{\n",
    "if (score>90)\n",
    "    print(\"A\")\n",
    "else if (score>80)\n",
    "    print(\"B\")\n",
    "else if (score>70)\n",
    "    print(\"C\")\n",
    "else if (score>60)\n",
    "    print(\"D\")\n",
    "else\n",
    "    print(\"F\")\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "450f67ca-f5c4-4e4f-8d95-d55a2a5fc149",
   "metadata": {},
   "source": [
    "上面这么写也没问题了，应该要确保if……else结构的完整性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "34b22be8-455e-4ab1-9d74-597b84a0cfec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "'Passed'"
      ],
      "text/latex": [
       "'Passed'"
      ],
      "text/markdown": [
       "'Passed'"
      ],
      "text/plain": [
       "[1] \"Passed\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "outcome <- ifelse(score>60,\"Passed\",\"Failed\")\n",
    "outcome"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "21175202-6bc8-402e-8c65-d780e576411e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"Passed\"\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "'Passed'"
      ],
      "text/latex": [
       "'Passed'"
      ],
      "text/markdown": [
       "'Passed'"
      ],
      "text/plain": [
       "[1] \"Passed\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ifelse(score>60,print(\"Passed\"),print(\"Failed\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "8f0f286b-e900-4590-8d89-c794857dafe8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"cheer up\"\n",
      "[1] \"nothing to fear\"\n"
     ]
    }
   ],
   "source": [
    "feelings = c(\"sad\",\"afraid\")\n",
    "for (i in feelings)\n",
    "    print(switch(i,\n",
    "                 happy=\"ok\",\n",
    "                 afraid=\"nothing to fear\",\n",
    "                 sad = \"cheer up\",\n",
    "                 angry = \"calm down\"\n",
    "                )\n",
    "         )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "550437d1-60fe-407f-9450-ae3209bff915",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 10 × 8</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>Student</th><th scope=col>Math</th><th scope=col>Science</th><th scope=col>English</th><th scope=col>MathZ</th><th scope=col>ScienceZ</th><th scope=col>EnglishZ</th><th scope=col>Zmean</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>John Davis       </td><td>502</td><td>95</td><td>25</td><td> 0.0127</td><td> 1.078</td><td> 0.5869</td><td> 0.559</td></tr>\n",
       "\t<tr><td>Angela Williams  </td><td>600</td><td>99</td><td>22</td><td> 1.1434</td><td> 1.591</td><td> 0.0367</td><td> 0.924</td></tr>\n",
       "\t<tr><td>Bullwinkle Moose </td><td>412</td><td>80</td><td>18</td><td>-1.0257</td><td>-0.847</td><td>-0.6969</td><td>-0.857</td></tr>\n",
       "\t<tr><td>David Jones      </td><td>358</td><td>82</td><td>15</td><td>-1.6487</td><td>-0.590</td><td>-1.2471</td><td>-1.162</td></tr>\n",
       "\t<tr><td>Janice Markhammer</td><td>495</td><td>75</td><td>20</td><td>-0.0681</td><td>-1.489</td><td>-0.3301</td><td>-0.629</td></tr>\n",
       "\t<tr><td>Cheryl Cushing   </td><td>512</td><td>85</td><td>28</td><td> 0.1281</td><td>-0.205</td><td> 1.1370</td><td> 0.353</td></tr>\n",
       "\t<tr><td>Reuven Ytzrhak   </td><td>410</td><td>80</td><td>15</td><td>-1.0488</td><td>-0.847</td><td>-1.2471</td><td>-1.048</td></tr>\n",
       "\t<tr><td>Greg Knox        </td><td>625</td><td>95</td><td>30</td><td> 1.4318</td><td> 1.078</td><td> 1.5038</td><td> 1.338</td></tr>\n",
       "\t<tr><td>Joel England     </td><td>573</td><td>89</td><td>27</td><td> 0.8319</td><td> 0.308</td><td> 0.9536</td><td> 0.698</td></tr>\n",
       "\t<tr><td>Mary Rayburn     </td><td>522</td><td>86</td><td>18</td><td> 0.2434</td><td>-0.077</td><td>-0.6969</td><td>-0.177</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 10 × 8\n",
       "\\begin{tabular}{llllllll}\n",
       " Student & Math & Science & English & MathZ & ScienceZ & EnglishZ & Zmean\\\\\n",
       " <chr> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl>\\\\\n",
       "\\hline\n",
       "\t John Davis        & 502 & 95 & 25 &  0.0127 &  1.078 &  0.5869 &  0.559\\\\\n",
       "\t Angela Williams   & 600 & 99 & 22 &  1.1434 &  1.591 &  0.0367 &  0.924\\\\\n",
       "\t Bullwinkle Moose  & 412 & 80 & 18 & -1.0257 & -0.847 & -0.6969 & -0.857\\\\\n",
       "\t David Jones       & 358 & 82 & 15 & -1.6487 & -0.590 & -1.2471 & -1.162\\\\\n",
       "\t Janice Markhammer & 495 & 75 & 20 & -0.0681 & -1.489 & -0.3301 & -0.629\\\\\n",
       "\t Cheryl Cushing    & 512 & 85 & 28 &  0.1281 & -0.205 &  1.1370 &  0.353\\\\\n",
       "\t Reuven Ytzrhak    & 410 & 80 & 15 & -1.0488 & -0.847 & -1.2471 & -1.048\\\\\n",
       "\t Greg Knox         & 625 & 95 & 30 &  1.4318 &  1.078 &  1.5038 &  1.338\\\\\n",
       "\t Joel England      & 573 & 89 & 27 &  0.8319 &  0.308 &  0.9536 &  0.698\\\\\n",
       "\t Mary Rayburn      & 522 & 86 & 18 &  0.2434 & -0.077 & -0.6969 & -0.177\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 10 × 8\n",
       "\n",
       "| Student &lt;chr&gt; | Math &lt;dbl&gt; | Science &lt;dbl&gt; | English &lt;dbl&gt; | MathZ &lt;dbl&gt; | ScienceZ &lt;dbl&gt; | EnglishZ &lt;dbl&gt; | Zmean &lt;dbl&gt; |\n",
       "|---|---|---|---|---|---|---|---|\n",
       "| John Davis        | 502 | 95 | 25 |  0.0127 |  1.078 |  0.5869 |  0.559 |\n",
       "| Angela Williams   | 600 | 99 | 22 |  1.1434 |  1.591 |  0.0367 |  0.924 |\n",
       "| Bullwinkle Moose  | 412 | 80 | 18 | -1.0257 | -0.847 | -0.6969 | -0.857 |\n",
       "| David Jones       | 358 | 82 | 15 | -1.6487 | -0.590 | -1.2471 | -1.162 |\n",
       "| Janice Markhammer | 495 | 75 | 20 | -0.0681 | -1.489 | -0.3301 | -0.629 |\n",
       "| Cheryl Cushing    | 512 | 85 | 28 |  0.1281 | -0.205 |  1.1370 |  0.353 |\n",
       "| Reuven Ytzrhak    | 410 | 80 | 15 | -1.0488 | -0.847 | -1.2471 | -1.048 |\n",
       "| Greg Knox         | 625 | 95 | 30 |  1.4318 |  1.078 |  1.5038 |  1.338 |\n",
       "| Joel England      | 573 | 89 | 27 |  0.8319 |  0.308 |  0.9536 |  0.698 |\n",
       "| Mary Rayburn      | 522 | 86 | 18 |  0.2434 | -0.077 | -0.6969 | -0.177 |\n",
       "\n"
      ],
      "text/plain": [
       "   Student           Math Science English MathZ   ScienceZ EnglishZ Zmean \n",
       "1  John Davis        502  95      25       0.0127  1.078    0.5869   0.559\n",
       "2  Angela Williams   600  99      22       1.1434  1.591    0.0367   0.924\n",
       "3  Bullwinkle Moose  412  80      18      -1.0257 -0.847   -0.6969  -0.857\n",
       "4  David Jones       358  82      15      -1.6487 -0.590   -1.2471  -1.162\n",
       "5  Janice Markhammer 495  75      20      -0.0681 -1.489   -0.3301  -0.629\n",
       "6  Cheryl Cushing    512  85      28       0.1281 -0.205    1.1370   0.353\n",
       "7  Reuven Ytzrhak    410  80      15      -1.0488 -0.847   -1.2471  -1.048\n",
       "8  Greg Knox         625  95      30       1.4318  1.078    1.5038   1.338\n",
       "9  Joel England      573  89      27       0.8319  0.308    0.9536   0.698\n",
       "10 Mary Rayburn      522  86      18       0.2434 -0.077   -0.6969  -0.177"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "student_test <- student_scores[,-9]\n",
    "student_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "ac9c10b7-91dc-49ca-9204-24ddc4edd9e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "levels <- quantile(student_test$Zmean,probs=seq(0,1,0.2))\n",
    "change_grade <- function(x){\n",
    "    if (x<=levels[2])\n",
    "        res <- \"F\"\n",
    "    else if (x<=levels[3])\n",
    "        res <- \"D\"\n",
    "    else if (x<= levels[4])\n",
    "        res <- \"C\"\n",
    "    else if (x<= levels[5])\n",
    "        res <- \"B\"\n",
    "    else\n",
    "        res <- \"A\"\n",
    "    return (res)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "e1075495-b48d-4709-84a3-2b33517eb1bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Student Math Science English   MathZ ScienceZ EnglishZ  Zmean\n",
      "1         John Davis  502      95      25  0.0127    1.078   0.5869  0.559\n",
      "2    Angela Williams  600      99      22  1.1434    1.591   0.0367  0.924\n",
      "3   Bullwinkle Moose  412      80      18 -1.0257   -0.847  -0.6969 -0.857\n",
      "4        David Jones  358      82      15 -1.6487   -0.590  -1.2471 -1.162\n",
      "5  Janice Markhammer  495      75      20 -0.0681   -1.489  -0.3301 -0.629\n",
      "6     Cheryl Cushing  512      85      28  0.1281   -0.205   1.1370  0.353\n",
      "7     Reuven Ytzrhak  410      80      15 -1.0488   -0.847  -1.2471 -1.048\n",
      "8          Greg Knox  625      95      30  1.4318    1.078   1.5038  1.338\n",
      "9       Joel England  573      89      27  0.8319    0.308   0.9536  0.698\n",
      "10      Mary Rayburn  522      86      18  0.2434   -0.077  -0.6969 -0.177\n"
     ]
    }
   ],
   "source": [
    "grade <- c()\n",
    "i = 1\n",
    "for (x in student_test$Zmean){\n",
    "    grade[i] <- change_grade(x)\n",
    "    i <- i+1\n",
    "}\n",
    "print(student_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "08e4569d-6f58-4cae-819f-001b003080c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Student Math Science English   MathZ ScienceZ EnglishZ  Zmean\n",
      "1         John Davis  502      95      25  0.0127    1.078   0.5869  0.559\n",
      "2    Angela Williams  600      99      22  1.1434    1.591   0.0367  0.924\n",
      "3   Bullwinkle Moose  412      80      18 -1.0257   -0.847  -0.6969 -0.857\n",
      "4        David Jones  358      82      15 -1.6487   -0.590  -1.2471 -1.162\n",
      "5  Janice Markhammer  495      75      20 -0.0681   -1.489  -0.3301 -0.629\n",
      "6     Cheryl Cushing  512      85      28  0.1281   -0.205   1.1370  0.353\n",
      "7     Reuven Ytzrhak  410      80      15 -1.0488   -0.847  -1.2471 -1.048\n",
      "8          Greg Knox  625      95      30  1.4318    1.078   1.5038  1.338\n",
      "9       Joel England  573      89      27  0.8319    0.308   0.9536  0.698\n",
      "10      Mary Rayburn  522      86      18  0.2434   -0.077  -0.6969 -0.177\n",
      "   grade\n",
      "1      B\n",
      "2      A\n",
      "3      D\n",
      "4      F\n",
      "5      D\n",
      "6      C\n",
      "7      F\n",
      "8      A\n",
      "9      B\n",
      "10     C\n"
     ]
    }
   ],
   "source": [
    "student_test$grade <- grade\n",
    "print(student_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "84f5f25f-13f9-40c1-852c-3f7c1ec04d76",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ?sapply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "53ffe3cd-ce83-48f7-8a8e-26c5f0f82323",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>'Student'</li><li>'Math'</li><li>'Science'</li><li>'English'</li><li>'MathZ'</li><li>'ScienceZ'</li><li>'EnglishZ'</li><li>'Zmean'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'Student'\n",
       "\\item 'Math'\n",
       "\\item 'Science'\n",
       "\\item 'English'\n",
       "\\item 'MathZ'\n",
       "\\item 'ScienceZ'\n",
       "\\item 'EnglishZ'\n",
       "\\item 'Zmean'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'Student'\n",
       "2. 'Math'\n",
       "3. 'Science'\n",
       "4. 'English'\n",
       "5. 'MathZ'\n",
       "6. 'ScienceZ'\n",
       "7. 'EnglishZ'\n",
       "8. 'Zmean'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"Student\"  \"Math\"     \"Science\"  \"English\"  \"MathZ\"    \"ScienceZ\" \"EnglishZ\"\n",
       "[8] \"Zmean\"   "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "student_test$grade <- NULL\n",
    "names(student_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "b861cc34-742b-498b-ac04-088527a632cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol>\n",
       "\t<li>'B'</li>\n",
       "\t<li>'A'</li>\n",
       "\t<li>'D'</li>\n",
       "\t<li>'F'</li>\n",
       "\t<li>'D'</li>\n",
       "\t<li>'C'</li>\n",
       "\t<li>'F'</li>\n",
       "\t<li>'A'</li>\n",
       "\t<li>'B'</li>\n",
       "\t<li>'C'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate}\n",
       "\\item 'B'\n",
       "\\item 'A'\n",
       "\\item 'D'\n",
       "\\item 'F'\n",
       "\\item 'D'\n",
       "\\item 'C'\n",
       "\\item 'F'\n",
       "\\item 'A'\n",
       "\\item 'B'\n",
       "\\item 'C'\n",
       "\\end{enumerate}\n"
      ],
      "text/markdown": [
       "1. 'B'\n",
       "2. 'A'\n",
       "3. 'D'\n",
       "4. 'F'\n",
       "5. 'D'\n",
       "6. 'C'\n",
       "7. 'F'\n",
       "8. 'A'\n",
       "9. 'B'\n",
       "10. 'C'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[[1]]\n",
       "[1] \"B\"\n",
       "\n",
       "[[2]]\n",
       "[1] \"A\"\n",
       "\n",
       "[[3]]\n",
       "[1] \"D\"\n",
       "\n",
       "[[4]]\n",
       "[1] \"F\"\n",
       "\n",
       "[[5]]\n",
       "[1] \"D\"\n",
       "\n",
       "[[6]]\n",
       "[1] \"C\"\n",
       "\n",
       "[[7]]\n",
       "[1] \"F\"\n",
       "\n",
       "[[8]]\n",
       "[1] \"A\"\n",
       "\n",
       "[[9]]\n",
       "[1] \"B\"\n",
       "\n",
       "[[10]]\n",
       "[1] \"C\"\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lapply(student_test$Zmean,change_grade)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeb4d62d-8f61-4d3f-a33d-f73fc44dc44c",
   "metadata": {},
   "source": [
    "最简单的应该就是用lapply了"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "206213e2-e56d-42f9-9db6-c2576d6285f2",
   "metadata": {},
   "source": [
    "转置、整合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "0be943e0-5c12-4e6f-80bb-74cc10de557c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   mpg cyl disp  hp\n",
      "Mazda RX4         21.0   6  160 110\n",
      "Mazda RX4 Wag     21.0   6  160 110\n",
      "Datsun 710        22.8   4  108  93\n",
      "Hornet 4 Drive    21.4   6  258 110\n",
      "Hornet Sportabout 18.7   8  360 175\n"
     ]
    }
   ],
   "source": [
    "cars<-mtcars[1:5,1:4]\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "ac069a49-88a3-43e6-96d4-97a90f4887a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A matrix: 4 × 5 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>Mazda RX4</th><th scope=col>Mazda RX4 Wag</th><th scope=col>Datsun 710</th><th scope=col>Hornet 4 Drive</th><th scope=col>Hornet Sportabout</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>mpg</th><td> 21</td><td> 21</td><td> 22.8</td><td> 21.4</td><td> 18.7</td></tr>\n",
       "\t<tr><th scope=row>cyl</th><td>  6</td><td>  6</td><td>  4.0</td><td>  6.0</td><td>  8.0</td></tr>\n",
       "\t<tr><th scope=row>disp</th><td>160</td><td>160</td><td>108.0</td><td>258.0</td><td>360.0</td></tr>\n",
       "\t<tr><th scope=row>hp</th><td>110</td><td>110</td><td> 93.0</td><td>110.0</td><td>175.0</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A matrix: 4 × 5 of type dbl\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & Mazda RX4 & Mazda RX4 Wag & Datsun 710 & Hornet 4 Drive & Hornet Sportabout\\\\\n",
       "\\hline\n",
       "\tmpg &  21 &  21 &  22.8 &  21.4 &  18.7\\\\\n",
       "\tcyl &   6 &   6 &   4.0 &   6.0 &   8.0\\\\\n",
       "\tdisp & 160 & 160 & 108.0 & 258.0 & 360.0\\\\\n",
       "\thp & 110 & 110 &  93.0 & 110.0 & 175.0\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A matrix: 4 × 5 of type dbl\n",
       "\n",
       "| <!--/--> | Mazda RX4 | Mazda RX4 Wag | Datsun 710 | Hornet 4 Drive | Hornet Sportabout |\n",
       "|---|---|---|---|---|---|\n",
       "| mpg |  21 |  21 |  22.8 |  21.4 |  18.7 |\n",
       "| cyl |   6 |   6 |   4.0 |   6.0 |   8.0 |\n",
       "| disp | 160 | 160 | 108.0 | 258.0 | 360.0 |\n",
       "| hp | 110 | 110 |  93.0 | 110.0 | 175.0 |\n",
       "\n"
      ],
      "text/plain": [
       "     Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout\n",
       "mpg   21        21            22.8       21.4           18.7            \n",
       "cyl    6         6             4.0        6.0            8.0            \n",
       "disp 160       160           108.0      258.0          360.0            \n",
       "hp   110       110            93.0      110.0          175.0            "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#转置\n",
    "t(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "4695c3a2-5806-408f-ab76-13f566321a6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Group.1 Group.2  mpg cyl disp  hp drat   wt qsec  vs   am gear carb\n",
      "1       4       3 21.5   4  120  97 3.70 2.46 20.0 1.0 0.00    3 1.00\n",
      "2       6       3 19.8   6  242 108 2.92 3.34 19.8 1.0 0.00    3 1.00\n",
      "3       8       3 15.1   8  358 194 3.12 4.10 17.1 0.0 0.00    3 3.08\n",
      "4       4       4 26.9   4  103  76 4.11 2.38 19.6 1.0 0.75    4 1.50\n",
      "5       6       4 19.8   6  164 116 3.91 3.09 17.7 0.5 0.50    4 4.00\n",
      "6       4       5 28.2   4  108 102 4.10 1.83 16.8 0.5 1.00    5 2.00\n",
      "7       6       5 19.7   6  145 175 3.62 2.77 15.5 0.0 1.00    5 6.00\n",
      "8       8       5 15.4   8  326 300 3.88 3.37 14.6 0.0 1.00    5 6.00\n"
     ]
    }
   ],
   "source": [
    "#整合其实就是groupby\n",
    "attach(mtcars)\n",
    "print(aggregate(mtcars,by=list(cyl,gear),FUN=mean,na.rm=TRUE))\n",
    "detach(mtcars)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3af450ab-65be-4a01-b966-d906a19b5c6b",
   "metadata": {},
   "source": [
    "融合、重铸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d2c9d2e5-8384-4351-afbc-aa5925a1f775",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "package 'reshape2' successfully unpacked and MD5 sums checked\n",
      "\n",
      "The downloaded binary packages are in\n",
      "\tC:\\Users\\xie.xiaokang\\AppData\\Local\\Temp\\Rtmpi888RY\\downloaded_packages\n"
     ]
    }
   ],
   "source": [
    "# install.packages(\"reshape2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "85409936-22ae-4684-b81b-61c47d13cc5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ID Time X1 X2\n",
      "1  1    1  5  6\n",
      "2  1    2  3  5\n",
      "3  2    1  6  1\n",
      "4  2    2  2  4\n"
     ]
    }
   ],
   "source": [
    "ID <- c(1,1,2,2)\n",
    "Time <- c(1,2,1,2)\n",
    "X1 <- c(5,3,6,2)\n",
    "X2 <- c(6,5,1,4)\n",
    "mydata <- data.frame(ID,Time,X1,X2)\n",
    "print(mydata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ae9fcecd-4937-4eaa-a82f-83cbb03af913",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ID Time variable value\n",
      "1  1    1       X1     5\n",
      "2  1    2       X1     3\n",
      "3  2    1       X1     6\n",
      "4  2    2       X1     2\n",
      "5  1    1       X2     6\n",
      "6  1    2       X2     5\n",
      "7  2    1       X2     1\n",
      "8  2    2       X2     4\n"
     ]
    }
   ],
   "source": [
    "library(reshape2)\n",
    "md <- melt(mydata,id=c(\"ID\",\"Time\"))\n",
    "print(md)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26e8e715-4c6f-4161-a1eb-d037ce117896",
   "metadata": {},
   "source": [
    "dcast()函数读取已经融合的数据，并使用提供的公式和用于整合数据的函数将其重塑。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "19caaa54-29d3-4301-b802-086fded55cb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ID   1 2\n",
      "1  1 5.5 4\n",
      "2  2 3.5 3\n"
     ]
    }
   ],
   "source": [
    "print(dcast(md,ID ~ Time,fun.aggregate=mean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c154b816-830a-4bc4-a082-309976198e43",
   "metadata": {},
   "outputs": [],
   "source": [
    "# install.packages(\"data.table\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58af4381-8fde-41c7-b6dd-31700b72e7e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# help(dcast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3cf43d63-17c3-4c1e-b35b-f404284b34ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ID X1  X2\n",
      "1  1  4 5.5\n",
      "2  2  4 2.5\n"
     ]
    }
   ],
   "source": [
    "print(dcast(md,ID ~ variable,fun.aggregate=mean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "faefa48f-a908-4300-8e60-24c8074d4684",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Time  X1  X2\n",
      "1    1 5.5 3.5\n",
      "2    2 2.5 4.5\n"
     ]
    }
   ],
   "source": [
    "print(dcast(md,Time ~ variable,fun.aggregate=mean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8a468817-5fc1-4496-b705-1de08bc4ba23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ID Time X1 X2\n",
      "1  1    1  5  6\n",
      "2  1    2  3  5\n",
      "3  2    1  6  1\n",
      "4  2    2  2  4\n"
     ]
    }
   ],
   "source": [
    "print(dcast(md,ID+Time ~ variable,fun.aggregate=mean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1097024c-2a25-40cb-b8dc-d7e73927c99a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ID X1_1 X1_2 X2_1 X2_2\n",
      "1  1    5    3    6    5\n",
      "2  2    6    2    1    4\n"
     ]
    }
   ],
   "source": [
    "print(dcast(md,ID ~ variable+Time,fun.aggregate=mean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebba3eac-74c9-4e96-9519-865d37ba4eb8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
